// pages/my/vip/index.js
const app=getApp();
const getVipCard=require("../../controllers/ControllerVip").controller.getVipCard;
const myInformation=require("../../controllers/ControllerVip").controller.myInformation;
const openCard=require("../../controllers/ControllerVip").controller.openCard;
const payMent=require("../../controllers/ControllerVip").controller.payMent;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    globalStype:'',
    value:{
      
    },
    cardList:[],
    yellow_bg:"../../../assets/my/img/vip_card_s.png",
    white_bg:"../../../assets/my/img/vip_activitu_s.png",
    vip:0,
    selectId:'',
    accountId:'',
    shopId:'',
    backbool:true,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (option) {
    this.setData({globalStype:app.globalData.globalStype})
    if (option.shopId) {
      this.setData({
        shopId: option.shopId
      })
      app.globalData.shopId = option.shopId
    }
    if(getCurrentPages().length<=1){
      this.setData({
        backbool:false
      })
    }else{
      this.setData({
        backbool:true
      })
    }
    
  },
  onShow(){
    if (wx.getStorageSync('usertoken')) {
        // 获取vip列表
        this.getCardList();
        this.getMyInformation();
    }
    //无usertoken，等待login登录回调
    app.usertokenReadyCallback = res => {
      if(res.data.sessionId){
        // 获取vip列表
        this.getCardList();
        this.getMyInformation();
      }
    }
    
  },
  //获取vipCard列表
   getCardList(){
     let shopId=app.globalData.shopId;
     shopId=parseInt(shopId);
     this.setData({
      shopId:shopId
     })
    getVipCard({shopId:shopId}).then((res)=>{
      this.setData({
        cardList:res.data
      })
    });
  },
  //跳转我的卡片页
  jumpToVip_myCard(){
    wx.navigateTo({
      url: '/pages/my/vip/vip_myCard/index',
    })
  },
  //进入使用说明页
  jumpVip_explain(e){
    let index=e.currentTarget.dataset.index;
    app.globalData.vipDesc=this.data.cardList[index].desc;
    wx.navigateTo({
      url: '/pages/my/vip/vip_explain/index',
    })
  },

  //获取自己详细信息
  getMyInformation(){
    let shopId=app.globalData.shopId;
    myInformation({shopId:shopId}).then(res=>{
      this.setData({
        vip:res.data.accountRelation.vip,
        accountId:res.data.accountRelation.accountId
      })
    })
  },

  //选中vip卡
  selectCard(e){
    let id=e.currentTarget.dataset.id;
    this.setData({
      selectId:id
    })
    console.log("触发");
    console.log(this.data.selectId);
  },

  //跳转详情
  jumpDetail(event){
    let id=event.currentTarget.dataset.id;
    // wx.navigateTo({
    //   url: '/pages/my/vip/vipCard_detail2/index?id='+id,
    // });
    wx.navigateTo({
      url: '/pages/my/vip/info/VIPCardInfo?id='+id,
    });
    
  },
  //pages/my/vip/info/VIPCardInfo

  //会员开卡
  getOpenCard(){
    if(this.data.selectId!=''){
      let shopId=app.globalData.shopId;
      let vipId=this.data.selectId;
      openCard({shopId:shopId,vipId:vipId}).then(res=>{
        if(res.code==1){
          let orderNo=res.data.orderNo;
          let openId=app.globalData.openId
          payMent({shopId:shopId,orderNo:orderNo,openId:openId,frontUrl:"mp:///pages/Index/Index"}).then(res=>{
              if(res.code==1){
                // let url=res.data;
                // url=encodeURIComponent(url);
                // console.log(url);
                // wx.navigateTo({
                //   url: '/pages/weixinlink/weixinlink?url='+url,
                // })
                // console.log(url);
                let data=JSON.parse(res.data);
                console.log(data)
                wx.requestPayment({
                  nonceStr: data.nonceStr,
                  package: data.package,
                  paySign: data.paySign,
                  timeStamp: data.timeStamp,
                  signType: data.signType,
                  success(res){
                    console.log(res)
                  },
                  fail(res){
                    console.log(res)
                  }
                })
              }
          })
        }else{
          wx.showToast({
            icon:"none",
            title: '开卡失败',
            duration:2000
          })
        }
      })
    }
  },
  onShareAppMessage: function (e) {
      let obj = {
        title: '',
        path:'/pages/Mall/Grouplists?shopId=' + this.data.shopId,
        success: function (res) {
          
        },
        fail(err) {
          
        }
      }
      return obj
  },
  onShareTimeline: function (e) {
    let obj = {
      title: '',
      path:'/pages/Mall/Grouplists?shopId=' + this.data.shopId,
      success: function (res) {
        
      },
      fail(err) {
        
      }
    }
    return obj
  },
  backindex(e){
    console.log(e)
    if (wx.getStorageSync('usertoken')) {
        wx.navigateTo({
          url:'/pages/Index/Index'
        })
    }else{
      app.GetLogin();
    }
  }
})